package xuesm.factory.simple;

import xuesm.factory.DellMouse;
import xuesm.factory.HPMouse;
import xuesm.factory.IMouse;

/**
 * @Author: xuesm
 * @Description:	简单工厂
 * @Date: Create in 20:23 2019/3/26
 */
public class SimpleMouseFactory {


	// 通过唯一标识创建对象
	public IMouse getMouseBean(String name){

		if (name != null && !"".equals(name)){
			if (name.equals("HP")){
				return new HPMouse();
			}else if (name.equals("DELL")){
				return new DellMouse();
			}else {
				System.out.println("该产品不存在");
			}
		}
		return null;
	}

	// 通过类名创建对象
	public IMouse getMouseBean2(String name){
		try {
			if (name != null && !"".equals(name)){
				return (IMouse) Class.forName(name).newInstance();
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return null;
	}


	// 通过字节码对象创建
	public IMouse getMouseBean3(Class<? extends IMouse> clazz){

		try {
			if (clazz != null){
				return clazz.newInstance();
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return null;
	}

}
